//
//  JLUserinfo.m
//  JLStyleApp
//
//  Created by LvJianfeng on 15/8/19.
//  Copyright (c) 2015年 JLStyle. All rights reserved.
//

#import "JLUserinfo.h"
@implementation JLUserinfo

/**
 * 注册 
 * @return int code 返回码 0成功 1失败
 * @return string msg 返回信息
 */
+ (void)do_register:(NSString*)phone password:(NSString*)password delegate:(id<JLUserinfoDelegate>)delegate{
    //网络
    NSString *postUrl = [IPURL stringByAppendingString:@"User.Register"];
    NSDictionary *params = @{@"phone":phone, @"password":password};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    //send request
    [manager GET:postUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON:%@",responseObject);
        NSDictionary *data = [responseObject objectForKey:@"data"];
        if ([data[@"code"] intValue]==0) {
            User *user = [User objectWithKeyValues:params];
            BOOL isOK = [User create:user];
            if (isOK) {
                [delegate operatingResult:isOK stateCode:0];
            }else{
                [delegate operatingResult:isOK stateCode:1];
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [delegate operatingResult:NO stateCode:1];
    }];
}

/**
 * 登录
 * @return int code 返回码 0成功 1不存在用户 2密码错误
 * @return string msg 返回信息
 */
+ (void)do_login:(NSString*)phone password:(NSString*)password delegate:(id<JLUserinfoDelegate>)delegate{
    NSString *postUrl = [IPURL stringByAppendingString:@"User.Login"];
    NSDictionary *params = @{@"phone":phone, @"password":password};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    //send request
    [manager GET:postUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON:%@",responseObject);
        NSDictionary *data = [responseObject objectForKey:@"data"];
        int code = [data[@"code"] intValue];
        [delegate operatingResult:YES stateCode:code];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [delegate operatingResult:NO stateCode:0];
    }];
}
@end
